const pool = require("../config/db");
// 返回所有记录

exports.getAll = async () => {
  const [rows] = await pool.query('SELECT * FROM yjstudent');
  return rows;
};

exports.getyid =  async(id)=>{
    const[rows]=await
  pool.query('SELECT * FROM yjstudent WHERE  id=?',[id]);
  return rows[0];
 
}
exports.getyname =  async(name)=>{
  console.log("getname:"+name)
  const[rows]=await pool.query('SELECT * FROM yjstudent WHERE name LIKE ?',[`%${name}%`])

return rows;

}
exports.del = async (id) => {
  const [result] = await pool.query('DELETE FROM yjstudent WHERE id = ?', [id]);
  return result;
};
exports.add = async (book) => {

  const [result] = await pool.query('INSERT INTO `yjstudent`(`id`, `name`) VALUES(?,?)', [book.id, book.name]);
 
  return result;
};

exports.update = async (id, book) => {
 
  const [currentBook] = await pool.query('SELECT * FROM yjstudent WHERE id = ?', [id]);
  // 合并更新后的字段与原始字段
  const mergedFields = { ...currentBook[0], ...book };
  const [result] = await pool.query('UPDATE yjstudent SET  id = ?, name = ?  WHERE id = ?', [mergedFields.id, mergedFields.name, id]);
  return result;
};

